<?php

namespace App\Http\Controllers\Platform\Setting;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Platform\Setting\Wallet\ItemRequest;
use App\Http\Resources\Common\DataResource;
use App\Models\Game\Platform\Org\Wallet;
use App\Models\Game\Platform\Org\Config;
use App\Services\Postman\GameService as PostmanGameService;
use App\Exceptions\Response\ServiceException;

class WalletController extends Controller
{

    /**
     * 获取数据
     */
    public function getData(Request $request)
    {
        $this->validate($request, [
            'plat_code' => 'required|string|max:64'
        ]);
        $platCode = $request->input('plat_code');

        $wallet = Wallet::withTrashed()->where('plat_code', $platCode)->firstOrFail();

        return new DataResource($wallet);
    }

    /**
     * 更新或创建数据
     *
     * @param string $platCode
     */
    public function updateOrCreate(ItemRequest $request)
    {
        $params = $request->only(['plat_code']);
        $data = $request->only(['sys_public_key', 'config']);

        if (!$request->input('id') || $request->input('update_key')) {
            $data += Wallet::makeKey($params['plat_code']);
        }

        $wallet = Wallet::withTrashed()->updateOrCreate($params, $data);

        // 通知游戏端配置修改
        $wallet->syncGameService();

        return new DataResource($wallet);
    }

    /**
     * 删除数据
     *
     * @param string $platCode
     */
    public function destroy(Request $request)
    {
        $this->validate($request, [
            'plat_code' => 'required|string|max:64'
        ]);
        $platCode = $request->input('plat_code');

        $wallet = Wallet::where('plat_code', $platCode)->firstOrFail();
        $wallet->delete();

        // 通知游戏端配置修改
        $wallet->syncGameService();

        return new DataResource($wallet);
    }

    /**
     * 恢复删除
     *
     * @param string $platCode
     */
    public function restore(Request $request)
    {
        $this->validate($request, [
            'plat_code' => 'required|string|max:64'
        ]);
        $platCode = $request->input('plat_code');

        $wallet = Wallet::withTrashed()->where('plat_code', $platCode)->firstOrFail();
        $wallet->restore();

        // 通知游戏端配置修改
        $wallet->syncGameService();

        return new DataResource($wallet);
    }

    /**
     * 验证钱包接口
     */
    public function verify(Request $request)
    {
        $params = $request->validate([
            'plat_code' => 'required|string|max:64',
            'account_id' => 'required|string|max:64',
        ]);

        $config = Config::where('plat_code', $params['plat_code'])->firstOrFail();
        if (empty($config->currency)) {
            throw new ServiceException(ServiceException::SERVICE_NOT_EXIST_PLAT_CURRENCY);
        }

        $message = PostmanGameService::post(PostmanGameService::URI_CHECK_WALLET,
            $params + ['currency' => $config->currency]
        );
        if (!empty($message)) {
            throw new ServiceException(null, $message);
        }

        return new DataResource(null);
    }
}
